package com.recharge.web.controller;

import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.RoleDataPermissionQueryDO;
import com.recharge.domain.vo.RoleDataPermissionDO;
import com.recharge.service.RoleDataPermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/roleDataPermission")
public class RoleDataPermissionController {

    @Autowired
    private RoleDataPermissionService roleDataPermissionService;

    @RequestMapping(value="/findField.html", method = RequestMethod.POST)
    @ApiOperation(value="根据表名获取字段", notes="")
    public @ResponseBody
    ObjectResponse<Object> findField(@RequestBody RoleDataPermissionQueryDO query) {
        ObjectResponse<Object> response = new ObjectResponse<>();
        ResultDO result = roleDataPermissionService.findField(query);
        if(result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/findTable.html", method = RequestMethod.POST)
    @ApiOperation(value="获取表", notes="")
    public @ResponseBody
    ObjectResponse<Object> findTable(@RequestBody RoleDataPermissionQueryDO query) {
        ObjectResponse<Object> response = new ObjectResponse<>();
        ResultDO result = roleDataPermissionService.findTable();
        if(result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    @ApiOperation(value="修改关联", notes="")
    public @ResponseBody
    ObjectResponse<Object> edit(@RequestBody RoleDataPermissionDO domain) {
        ObjectResponse<Object> response = new ObjectResponse<>();
        ResultDO result = roleDataPermissionService.edit(domain);
        if(result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
}
